package com.kjlink.widget.oa.helper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kjlink.widget.oa.enums.OaApiUrlEnum;
import com.kjlink.widget.oa.enums.OaApiUrlInterface;
import com.kjlink.widget.oa.model.OAProject;
import com.kjlink.widget.oa.model.OaDepartment;
import com.kjlink.widget.oa.model.OaUser;

import java.util.List;

/**
 * 描述:
 *
 * @author Vic.xu
 * @since 2025-09-01 15:56
 */
public class TestRequest {

    static OaApiHelper oaApiHelper = new OaApiHelper();

    public static void main(String[] args) {
        String last = "2024-11-01";
        for (OaApiUrlEnum value : OaApiUrlEnum.values()) {
            OaApiHelper.getLastRequestTimeMap().put(value.getUrl(), last);
        }
        testDept();
//        testProject();
//        testUser();
    }


    public static void testProject(){
        List<OAProject> projectList = oaApiHelper.syncFromOa(OaApiUrlEnum.PROJECT_SYNC);
        printJson(projectList);

    }

    // syncData(OaApiUrlEnum.BO_PROJECT_SYNC);
    public static void testBoProject(){
        List<OAProject> boProjectList = oaApiHelper.syncFromOa(OaApiUrlEnum.BO_PROJECT_SYNC);
        printJson(boProjectList);
    }

    public static void testUser(){
        List<OaUser> userList = oaApiHelper.syncFromOa(OaApiUrlEnum.USER_SYNC);
        printJson(userList);
    }

    public static void testDept(){
        List<OaDepartment> departmentList = oaApiHelper.syncFromOa(OaApiUrlEnum.DEPT_SYNC);
        printJson(departmentList);
    }

    private static void syncData(OaApiUrlInterface urlEnum) {
        // 项目
        List<OAProject> projectList = oaApiHelper.syncFromOa(OaApiUrlEnum.PROJECT_SYNC);
        System.out.println("项目 " + projectList.size());
        // 打印no  name
        projectList.forEach(project -> System.out.println(project.getProjectNo() + "  " + project.getProjectName()));
        // 商机
        List<OAProject> boProjectList = oaApiHelper.syncFromOa(OaApiUrlEnum.BO_PROJECT_SYNC);
        System.out.println("商机:" + boProjectList.size());
        boProjectList.forEach(project -> System.out.println(project.getProjectNo() + "  " + project.getProjectName()));
    }

    private static void printJson(Object obj) {
        if (obj instanceof List) {
            // size
            System.out.println("size:" + ((List<?>) obj).size());
        }
        if (obj instanceof String) {
            System.out.println(obj);
            return;
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }


}
